﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using Snowdreamist.Windows.Data;
using Snowdreamist.Windows.Notification;
using SNSpirit.Service.Certificate;
using SNSpirit.Service.Navigation;

namespace SNSpirit.ViewModel.Authentication
{
    public abstract class NamePasswordAuthenticationViewModel : ViewModelBase
    {
        public NamePasswordAuthenticationViewModel() : this(null) { }

        public NamePasswordAuthenticationViewModel(IEnumerable<INamePasswordCertificate> savedCertificates)
        {
            this.Certificates = savedCertificates;
        }

        #region Username

        private string _Username;

        /// <summary>
        /// Username
        /// </summary>
        public string Username
        {
            get { return this._Username; }
            set
            {
                if (this._Username != value)
                {
                    this._Username = value;
                    this.OnPropertyChanged("Username");
                }
            }
        }

        #endregion Username

        #region IsSaveUsername

        private bool _IsSaveUsername;

        /// <summary>
        /// IsSaveUsername
        /// </summary>
        public bool IsSaveUsername
        {
            get { return this._IsSaveUsername; }
            set
            {
                if (this._IsSaveUsername != value)
                {
                    this._IsSaveUsername = value;
                    this.OnPropertyChanged("IsSaveUsername");
                }
            }
        }

        #endregion IsSaveUsername

        #region IsSaveCertificate

        private bool _IsSaveCertificate;

        /// <summary>
        /// IsSaveCertificate
        /// </summary>
        public bool IsSaveCertificate
        {
            get { return this._IsSaveCertificate; }
            set
            {
                if (this._IsSaveCertificate != value)
                {
                    this._IsSaveCertificate = value;
                    this.OnPropertyChanged("IsSaveCertificate");
                }
            }
        }

        #endregion IsSaveCertificate

        public IEnumerable<INamePasswordCertificate> Certificates { get; private set; }

        public Func<SecureString> GetPasswordFunction { get; set; }

        public virtual INamePasswordCertificate GetCertificate()
        {
            if (GetPasswordFunction == null)
                throw new InvalidOperationException("Cannot get password");
            return new NamePasswordCertificate(this.Username, this.GetPasswordFunction());
        }

        public virtual object GetAdditionalData()
        {
            return null;
        }

        public void NotifyCertificateError()
        {
            // notify
            SNSpirit.Service.Notification.NotificationService.Current.Notify(
                NotificationScope.Application,
                NotificationType.Error,
                InteractiveType.User,
                LanguageManager.Current.GetResource(Utility.AuthenticationCertificateErrorStringKey)
                );
            // reset UI
            // TODO
        }

        public void Login()
        {
            if (this.StartLogin != null)
                this.StartLogin(this, new EventArgs());
        }

        public void Cancel()
        {
            if (this.Cancelled != null)
                this.StartLogin(this, new EventArgs());
        }

        public event EventHandler StartLogin;

        public event EventHandler Cancelled;
    }
}